# coding: utf-8
# Author: Legend奇
# @Time： 2020/7/21 下午3:32
from gettext import gettext as _

from tortoise import fields

from apps.log.operations import OPERATION_TYPE, OPERATION_SORT
from base.base_model import MyAbstractBaseModel
from utils.time import get_current_time_by_microsecond


class OperationLog(MyAbstractBaseModel):
    username = fields.CharField(max_length=255, default='', verbose_name=_('user name'))
    ipv4 = fields.CharField(max_length=15, default='', null=True, verbose_name=_('user ipv4 address'))
    ipv6 = fields.CharField(max_length=25, default='', null=True, verbose_name=_('user ipv6 address'))
    op_type = fields.SmallIntField(default=0, choices=OPERATION_TYPE, verbose_name=_('operation type'))
    op_sort = fields.SmallIntField(default=0, choices=OPERATION_SORT, verbose_name=_('operation sort'))
    origin_data = fields.TextField(default='', verbose_name=_('origin data'))
    desc = fields.TextField(default='', verbose_name=_('description'))
    create_timestamp = fields.BigIntField(default=get_current_time_by_microsecond, auto_now_add=True,
                                          verbose_name=_('create time'))

    class Meta:
        table = 'operationlog'
        verbose_name = _('operation log')


class UserOperation(MyAbstractBaseModel):
    device_id = fields.IntField(null=True, verbose_name=_('device id'))
    collect_timestamp = fields.BigIntField(default=get_current_time_by_microsecond,
                                           verbose_name=_('collect time point'))
    from_ip = fields.CharField(default='', null=False, max_length=120)
    command = fields.TextField(default='', null=False)
    user = fields.CharField(default='', null=False, max_length=120)
    pwd = fields.CharField(default='', null=False, max_length=120)
    operation_time = fields.BigIntField(default=0, null=False)

    class Meta:
        table = 'user_operation'
        verbose_name = _('user operation info')
